//
// Created by qianyi on 2023/4/28.
//

#include "stdio.h"
#include "BzDB.h"
#include "utils/BzDB_array_operate.h"
#include "utils/BzDB_string_operate.h"
#include "utils/BzDB_file_operate.h"
#include "3rd_utils/cJSON/cJSON.h"

// private method =================================
// private method =================================
int BzDB_find_col_name_index(BzDB_Table_t * dbTable , const char* row_name);
// 判断某一行是否满足条件
int BzDB_private_isOneRow_string_OK(BzDB_Table_t * dbTable,int row_index , const char * col_name , const char* op ,const char * value );
int BzDB_private_isOneRow_integer_OK(BzDB_Table_t * dbTable,int row_index , const char * col_name , const char * op , int value );
// 打印一行数据
void BzDB_private_printOneRow(BzDB_Table_t * dbTable,int row_index);
// private method =================================
// private method =================================





// =====================================
void BzDB_String_set(BzDB_String_t* bz_str , const char * msg)
{
    strcpy(bz_str->data,msg);
}
void BzDB_Integer_set(BzDB_Integer_t *bz_int , int value){
    bz_int->data = value;
}
const char * BzDB_Value_get_string(BzDB_Value_t *value){
    return value->valuestring.data;
}
int BzDB_Value_get_integer(BzDB_Value_t *value){
    return value->valueint.data;
}
void BzDB_Value_set_string(BzDB_Value_t *value , const char * msg){
    strcpy(value->valuestring.data , msg);
}
void BzDB_Value_set_integer(BzDB_Value_t *value , int valueint){
    value->valueint.data = valueint;
}

// =====================================================

// ======================================================
// ======================================================
// 判断某一行是否满足条件(string)
int BzDB_private_isOneRow_string_OK(BzDB_Table_t * dbTable,int row_index , const char * col_name , const char* op ,const char * value ){
    BzDB_Row_t* row = &(dbTable->row_list[row_index]);

    int col_index = BzDB_find_col_name_index(dbTable,col_name);

    if(col_index == -1){
        return 0;
    }

    const char * str_in_dataset = row->value[col_index].valuestring.data;


    if(strcmp(op,"==") == 0){
        if (strcmp(str_in_dataset,value)==0){
            return 1;
        }else{
            return 0;
        }
    }
    else if(strcmp(op,"contain")==0){
        // 在str_in_dataset 中寻找是否包含value，如果有则返回下标，否则返回-1
        int res = BzDB_string_find_first(str_in_dataset, value,0);
        if(res >= 0){
            return 1;
        } else{
            return 0;
        }
    }

    return 0;

}
// 判断某一列是否满足条件(string)
int BzDB_private_isOneRow_integer_OK(BzDB_Table_t * dbTable,int row_index , const char * col_name , const char * op , int value ){
    BzDB_Row_t* row = &(dbTable->row_list[row_index]);

    int col_index = BzDB_find_col_name_index(dbTable,col_name);

    if(col_index == -1){
        return 0;
    }

    if(strcmp(op,"==")==0)
    {
        return row->value[col_index].valueint.data == value;
    }
    if(strcmp(op,">")==0)
    {
        return row->value[col_index].valueint.data > value;
    }
    if(strcmp(op,">=")==0)
    {
        return row->value[col_index].valueint.data >= value;
    }
    if(strcmp(op,"<")==0)
    {
        return row->value[col_index].valueint.data < value;
    }
    if(strcmp(op,"<=")==0)
    {
        return row->value[col_index].valueint.data <= value;
    }
    return 0;
}
// 打印一行数据
void BzDB_private_printOneRow(BzDB_Table_t * dbTable,int row_index){
    BzDB_Row_t* row = &(dbTable->row_list[row_index]);
    for(int j = 0 ; j < dbTable->used_col_count ; j++)
    {
        BzDB_Value_t *value =  &(row->value[j]);

        if(dbTable->col_names_type[j] == STRING)
        {
            printf("%s\t",value->valuestring.data);
        }
        else if(dbTable->col_names_type[j] == INTEGER)
        {
            printf("%d\t",value->valueint.data);
        }

    }
}
int BzDB_Row_t_get_by_col_name(BzDB_Table_t * table ,BzDB_Row_t *row , const char * col_name,BzDB_Value_t *value)
{

    int index = BzDB_find_col_name_index(table,col_name);
    if(index < 0){
        return 0;
    }else{
        memset(value,0,sizeof(BzDB_Value_t));
        memcpy(value,&(row->value[index]),sizeof(BzDB_Value_t));
    }

}

// =======================================================
// =======================================================











// ========================================================

/**
 *
 * @param dbTable
 * @param col_num
 * @param col_name_list
 * @param col_name_type_list
 */
void BzDB_Table_t_init(BzDB_Table_t * dbTable ,int col_num , const char* col_name_list[] ,BzDB_ValueType col_name_type_list[] ){
    // init row_list
    for(int i = 0 ; i < BzDB_MAX_ROW_COUNT ; i++){
        memset(&(dbTable->row_list[i]),0,sizeof(BzDB_Row_t));
    }

    dbTable->max_col_count = BzDB_MAX_COL_COUNT;
    dbTable->max_row_count = BzDB_MAX_ROW_COUNT;

//    初始化BzDB_string
    // 初始化列名
    for(int i = 0 ; i < col_num ; i++)
    {
        // 字符串 复制
        strcpy(dbTable->col_names[i].data , col_name_list[i]);
//        cout << dbTable->col_names[i].data << endl;
    }

    // 初始化列的类型
    for(int i = 0 ; i < col_num ; i++)
    {
        // 字符串 复制
        dbTable->col_names_type[i] = col_name_type_list[i];
//        cout << dbTable->col_names_type[i] << endl;
    }

    dbTable->used_col_count = col_num;
    dbTable->used_row_count = 0;

}

void BzDB_insert(BzDB_Table_t * dbTable , void * row_value[])
{
    int used_row_count = dbTable->used_row_count;
    // 定义一个行
    BzDB_Row_t row;

    // 遍历每一列
    for(int i = 0 ; i < dbTable->used_col_count ; i++)
    {
        // 定义一个值
        BzDB_Value_t value;

        // 如果是字符串
        if(dbTable->col_names_type[i] == STRING)
        {
            BzDB_String_t* temp = (BzDB_String_t*)row_value[i];
            BzDB_String_set(&value.valuestring,temp->data);
        }
            // 如果是整数
        else if(dbTable->col_names_type[i]==INTEGER)
        {
            BzDB_Integer_t* temp = (BzDB_Integer_t*)row_value[i];
            BzDB_Integer_set(&value.valueint,temp->data);
        }

        memcpy(&row.value[i] , &value, sizeof(value) );
    }
    memcpy(&dbTable->row_list[used_row_count],&row,sizeof(row));
    dbTable->used_row_count = dbTable->used_row_count+1;
}




int BzDB_find_col_name_index(BzDB_Table_t * dbTable , const char* row_name)
{
    for(int i = 0 ; i < dbTable->used_col_count ; i++){
        if(strcmp(dbTable->col_names[i].data , row_name) == 0){
            return i;
        }
    }
    return -1;
}

void BzDB_showAll(BzDB_Table_t * dbTable)
{

    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        BzDB_private_printOneRow(dbTable,i);
        printf("\n");
    }
}

/**
 *  BzDB_select_by_string(&table,"name","contain","lisi",res,&res_count);
 * @param dbTable 数据库
 * @param col_name 列名
 * @param value   值
 * @param res     返回的结果
 */
void BzDB_select_by_string(BzDB_Table_t * dbTable,const char* col_name ,const char* op, const char* value , BzDB_Row_t res[],int *res_count)
{
    *res_count = 0;
    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        int is_ok = BzDB_private_isOneRow_string_OK(dbTable,i,col_name,op,value);

        if(is_ok){
            // 如果满足条件
            memcpy(&(res[*res_count]),&(dbTable->row_list[i]),sizeof(BzDB_Row_t));
            *res_count = *res_count + 1;
        }
    }
}

/**
 *  根据intger进行select
 * @param dbTable
 * @param col_name
 * @param op
 * @param value
 * @param res
 * @param res_count
 */
void BzDB_select_by_integer(BzDB_Table_t * dbTable,const char* col_name , const char* op , int value,BzDB_Row_t res[],int *res_count)
{
    *res_count = 0;
    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        int is_ok = BzDB_private_isOneRow_integer_OK(dbTable,i,col_name,op,value);
        if(is_ok){
            // 如果满足条件
            memcpy(&(res[*res_count]),&(dbTable->row_list[i]),sizeof(BzDB_Row_t));
            *res_count = *res_count + 1;
        }
    }
}


/**
 *   min_value <= x <= max_value  左闭右闭
 *
 *     BzDB_Row_t res2[20];// 最多查询出20条，多出来会出错
        int res_count2 = 0;
        BzDB_select_by_integer_between(&table,"age" ,20,30,res2,&res_count2);
 * @param dbTable
 * @param col_name
 * @param min_value
 * @param max_value
 * @param res
 * @param res_count
 */
void BzDB_select_by_integer_between(BzDB_Table_t * dbTable,
                                    const char* col_name ,
                                    int min_value,
                                    int max_value,
                                    BzDB_Row_t res[],
                                    int *res_count)
{
    *res_count = 0;
    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        int is_ok1 = BzDB_private_isOneRow_integer_OK(dbTable,i,col_name,">=",min_value);
        int is_ok2 = BzDB_private_isOneRow_integer_OK(dbTable,i,col_name,"<=",max_value);
        if(is_ok1 && is_ok2){
            // 如果满足条件
            memcpy(&(res[*res_count]),&(dbTable->row_list[i]),sizeof(BzDB_Row_t));
            *res_count = *res_count + 1;
        }
    }
}


/**
 * 根据条件进行查询，条件最多为99个
 * @param dbTable
 * @param condition_list
 * @param condition_list_len
 * @param res
 * @param res_count
 * @param connect_type
 */
void BzDB_select_by_multiple_condition(BzDB_Table_t * dbTable ,
                                       BzDB_Select_Condition_t condition_list[],
                                       int condition_list_len ,
                                       BzDB_Row_t res[],
                                       int *res_count,
                                       const char* connect_type
)
{

    *res_count = 0;

    if(condition_list_len >= 100){
        printf("condition_list_len的长度最多为99");
        return;
    }

    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        // 对每一行进行遍历
        int is_ok_list[100] = {0};
        int is_ok_index = 0;

        // 对每一个条件进行遍历

        for(int j = 0 ; j < condition_list_len ; j++){
            BzDB_Select_Condition_t *tmp_c =  &condition_list[j];
            int is_ok  = -1;
            if(tmp_c->value_type == INTEGER){
                is_ok = BzDB_private_isOneRow_integer_OK(dbTable,i,tmp_c->col_name,tmp_c->op,tmp_c->value_int);
            } else if(tmp_c->value_type == STRING){
                is_ok = BzDB_private_isOneRow_string_OK(dbTable,i,tmp_c->col_name,tmp_c->op,tmp_c->value_string);
            }
            is_ok_list[is_ok_index] = is_ok;
            is_ok_index = is_ok_index + 1;
        }

        int is_ok = 0;
        if(strcmp(connect_type,"||")==0){
            // 判断是||
            is_ok = BzDB_array_sum(is_ok_list, is_ok_index);
        }else if(strcmp(connect_type,"&&")==0){
            // 判断是&&
            is_ok = BzDB_array_multiple(is_ok_list, is_ok_index);
        }

        if(is_ok){
            // 如果满足条件
            memcpy(&(res[*res_count]),&(dbTable->row_list[i]),sizeof(BzDB_Row_t));
            *res_count = *res_count + 1;
        }
    }



}




void BzDB_Row_Intersection(BzDB_Row_t rows1[], int rows1_len , BzDB_Row_t rows2[], int rows2_len , BzDB_Row_t res_rows[] , int* res_rows_len){

    *res_rows_len = 0;

    for(int i = 0 ; i < rows1_len ; i++){
        for(int j = 0 ; j < rows2_len ; j++){
            int res = memcmp( &(rows1[i]) , &(rows2[j]) , sizeof(BzDB_Row_t) );
//            int res = memcmp( &(rows1[i]) , &(rows1[i]) , sizeof(BzDB_Row_t) );
            if(res == 0 ){
                memcpy(&(res_rows[*res_rows_len]) , &(rows1[i]) , sizeof(BzDB_Row_t) );
                *res_rows_len = *res_rows_len + 1;
                break;
            }
        }
    }
}


void BzDB_Row_Union(BzDB_Row_t rows1[], int rows1_len , BzDB_Row_t rows2[], int rows2_len , BzDB_Row_t res_rows[] , int* res_rows_len){

    *res_rows_len = 0;


    // 先把rows2的复制进去
    for(int i = 0 ; i < rows2_len ; i++){
        memcpy( &(res_rows[*res_rows_len]) , &(rows2[i]) , sizeof(BzDB_Row_t));
        *res_rows_len = *res_rows_len + 1;
    }

    // 再把row1中不在row2中的数据添加进去
    for(int i = 0 ; i < rows1_len ; i++){
        int flag = 1 ;
        for(int j = 0 ; j < rows2_len ; j++){
            int res = memcmp( &(rows1[i]) , &(rows2[j]) , sizeof(BzDB_Row_t) );
            if(res == 0 ){
                flag = 0; // 说明row1中有数据是在row2之中的，则不添加
            }
        }
        if(flag == 1 ){
            // 说明row1中有数据都不在row2之中的，可以添加
            memcpy(&(res_rows[*res_rows_len]) , &(rows1[i]) , sizeof(BzDB_Row_t) );
            *res_rows_len = *res_rows_len + 1;
            break;
        }

    }
}


void BzDB_delete(BzDB_Table_t * dbTable )
{
    // 先查询之后，然后再更新
}


void BzDB_private_delete_oneLine_by_index(BzDB_Table_t * dbTable , int index_)
{
    for(int i = index_ ; i < (dbTable->used_row_count - 1) ; i++){
        // i+1 赋值给 i
        memcpy( &(dbTable->row_list[i]) , &(dbTable->row_list[i+1]) , sizeof(BzDB_Row_t));
    }
    int last_index = (dbTable->used_row_count - 1);
    memset(&(dbTable->row_list[last_index]) , 0 , sizeof(BzDB_Row_t));
    dbTable->used_row_count = dbTable->used_row_count -1;
}

void BzDB_delete_by_index(BzDB_Table_t * dbTable , int index_list[] , int index_list_len)
{
    // 先对index进行降序排序
    BzDB_array_sort(index_list , index_list_len);
    BzDB_array_reverse(index_list,index_list_len);

    // 先查询之后，然后再更新
    for(int i = 0 ; i < index_list_len ; i++){
        // 删除第i行
        BzDB_private_delete_oneLine_by_index(dbTable , index_list[i]);
    }
}

// 在table中查找是否有这一行，如果没有则返回-1，有的话则返回其下标
int BzBD_findOneLine_by_Row_t(BzDB_Table_t * dbTable, BzDB_Row_t *row){
    for(int i = 0 ; i < dbTable->used_row_count ; i++){
        int res = memcmp( &(dbTable->row_list[i]) , row , sizeof(BzDB_Row_t));
        if(res == 0){
            return i;
        }
    }
    return -1;
}

void BzDB_select_index_by_Row_t(BzDB_Table_t * dbTable, BzDB_Row_t selected_rows[] , int row_len , int index_list[],int* index_list_len)
{
    *index_list_len = 0;
    // 先查询之后，然后再更新
    for(int i = 0 ; i < row_len ; i++){
        int index = BzBD_findOneLine_by_Row_t(dbTable, &(selected_rows[i]));
        if(index > -1){
            index_list[*index_list_len] = index;
            *index_list_len = *index_list_len + 1;
        }
    }
}

void BzDB_update(){

}

void BzDB_update_by_index_list(BzDB_Table_t *dbTable , int index_list[],int index_list_len,const char* col_name,void * value_in)
{
    // step3: 用下标值进行更新
    for(int i = 0 ; i < index_list_len ; i++){
// 获取需要修改的行
        int row_index_ = index_list[i]; // 获取需要修改的行下标
        BzDB_Row_t * row = &(dbTable->row_list[row_index_]); // 获取当前行

// 获取需要修改的列
        int col_index = BzDB_find_col_name_index(dbTable,col_name); // 获取name所在的列下标
        BzDB_Value_t *value = &(row->value[col_index]);    // 获取当前列

        if(dbTable->col_names_type[col_index]==STRING){
            // 赋值STRING
            BzDB_Value_set_string(value,(const char *)value_in); // 给当前列赋值
        } else if(dbTable->col_names_type[col_index]==INTEGER){
            // 赋值INTEGER
            BzDB_Value_set_integer(value,*((int*)value_in));
        }


    }
}


// ===============================================================






void BzDB_update_by_index_list_user_op(BzDB_Table_t *dbTable , int index_list[],int index_list_len,const char* col_name,void* value_in,void (*user_op)(BzDB_Value_t *v_bz,void * value_in))
{
    // step3: 用下标值进行更新
    for(int i = 0 ; i < index_list_len ; i++){
        // 获取需要修改的行
        int row_index_ = index_list[i]; // 获取需要修改的行下标
        BzDB_Row_t * row = &(dbTable->row_list[row_index_]); // 获取当前行

        // 获取需要修改的列
        int col_index = BzDB_find_col_name_index(dbTable,col_name); // 获取name所在的列下标
        BzDB_Value_t *value = &(row->value[col_index]);    // 获取当前列

        user_op(value,value_in);
    }
}




















void BzDB_insert_one_row_from_str(BzDB_Table_t *table,const char* msg)
{
    // 设置值
    BzDB_String_t v_str[BzDB_MAX_COL_COUNT] ;
    BzDB_Integer_t v_int[BzDB_MAX_COL_COUNT] ;

    void * row[BzDB_MAX_COL_COUNT] ;

    int start_index_list[BzDB_MAX_COL_COUNT] = {0};
    int end_index_list[BzDB_MAX_COL_COUNT] = {0};
    int len = 0;
    BzDB_string_split(msg, "|" , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++) {
        char temp[255] = {0};
        BzDB_string_sub(msg, start_index_list[i], end_index_list[i], temp);

        if(table->col_names_type[i]==STRING){
            BzDB_String_set(&(v_str[i]),temp);
            row[i] = &(v_str[i]);
        }

        if(table->col_names_type[i]==INTEGER){

            int res = BzDB_string_paser_str_to_int(temp);
            BzDB_Integer_set(&(v_int[i]),res);
            row[i] = &(v_int[i]);
        }


    }

    BzDB_insert(table,row); // insert

}


// ================= 从txt文件中读取数据 ==============
// =================================================
// =================================================
// =================================================
// =================================================
/**
 *     BzDB_Table_t table;
    const char* file_path = "C:\\Users\\qianyi\\CLionProjects\\BzDB\\input.txt";
    BzDB_create_table_from_txt(&table,file_path);
 * @param table
 * @param file_path
 */
#define BZDB_MAX_CONTENT_LEN 6000
void BzDB_create_table_from_txt(BzDB_Table_t *table,const char* file_path)
{

    char buf_temp[BZDB_MAX_CONTENT_LEN];
    char buf[BZDB_MAX_CONTENT_LEN];
    BzDB_file_operate_read_content( file_path, buf_temp,6000);

    BzDB_string_replace(buf_temp,"\r","",buf);

    char row_1_str[512] = {0}; // 存放第一行数据的
    char row_2_str[512] = {0}; // 存放第二行数据的
    char data_str[BZDB_MAX_CONTENT_LEN] = {0}; // 存放数据的

    // 按照\n进行分割
    int start_index_list[BzDB_MAX_COL_COUNT] = {0};
    int end_index_list[BzDB_MAX_COL_COUNT] = {0};
    int len = 0;
    BzDB_string_split(buf, "\n" , start_index_list,end_index_list,&len);
    int row_index = 1;
    for(int i = 0 ; i < len ; i++){
        char temp[255] = {0};
        BzDB_string_sub(buf,start_index_list[i],end_index_list[i],temp);

        if(strlen(temp)<2){
            // 如果＜2，说明没有值，直接退出
            continue;
        }

        if(row_index == 1){
            // 这是第一行
            strcpy(row_1_str,temp);
        }
        if(row_index == 2){
            // 这是第二行
            strcpy(row_2_str,temp);
        }

        if(row_index > 2){
            strcat(data_str,temp);
            strcat(data_str,"\n");
        }
        row_index = row_index + 1;
    }


    const char* names[BzDB_MAX_COL_COUNT] ;
    char names_array[BzDB_MAX_COL_COUNT][100] ;

    len = 0;
    BzDB_string_split(row_1_str, "," , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++){
        char temp[255] = {0};
        BzDB_string_sub(row_1_str,start_index_list[i],end_index_list[i],temp);

        strcpy(names_array[i],temp);
        names[i] = (const char*)names_array[i];
    }


    len = 0;
    BzDB_ValueType types[BzDB_MAX_COL_COUNT] ;
    BzDB_string_split(row_2_str, "," , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++){
        char temp[255] = {0};
        BzDB_string_sub(row_2_str,start_index_list[i],end_index_list[i],temp);

        if( strcmp(temp,"STRING") == 0 ){
            types[i] = STRING;
        }
        if( strcmp(temp,"INTEGER") == 0 ){
            types[i] = INTEGER;
        }
    }

    // init

    BzDB_Table_t_init(table,len ,names ,types);

    len = 0;
    BzDB_string_split(data_str, "\n" , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++){
        char temp[1024] = {0};
        BzDB_string_sub(data_str,start_index_list[i],end_index_list[i],temp);

        if(strlen(temp)<2){
            continue;
        }
        BzDB_insert_one_row_from_str(table,temp);
    }

}


/**
 *     BzDB_Table_t table;
    const char* file_path = "C:\\Users\\qianyi\\CLionProjects\\BzDB\\input.txt";
    BzDB_create_table_from_txt(&table,file_path);
    // 写入txt文件中
    BzDB_save_table_to_txt(&table,"C:\\Users\\qianyi\\CLionProjects\\BzDB\\output.txt");
 * @param table
 * @param file_path
 */
void BzDB_save_table_to_txt(BzDB_Table_t *table,const char* file_path)
{
    FILE *fp = fopen(file_path,"w");
    if(fp==NULL){
        return;
    }

    for(int i = 0 ; i < table->used_col_count ; i++)
    {
        char temp_name[255] = {0};
        strcpy(temp_name,(table->col_names)[i].data);
        printf("%s,temp_name\n",temp_name);
        if(i == table->used_col_count-1){
            fprintf(fp,"%s",temp_name);
        }else{
            fprintf(fp,"%s,",temp_name);
        }
    }
    fprintf(fp,"\n");

    for(int i = 0 ; i < table->used_col_count ; i++)
    {

        BzDB_ValueType t_ = (table->col_names_type)[i];

        if(t_ == INTEGER){
            fprintf(fp,"INTEGER");
        }else if(t_ == STRING){
            fprintf(fp,"STRING");
        }

        if(i < table->used_col_count-1){
            fprintf(fp,",");
        }
    }
    fprintf(fp,"\n");


    for(int i = 0 ; i < table->used_row_count ; i++)
    {
        for (int j = 0; j < table->used_col_count; j++)
        {
            if(table->col_names_type[j]==INTEGER){
                fprintf(fp,"%d",table->row_list[i].value[j].valueint.data);
                printf("%d",table->row_list[i].value[j].valueint.data);
            }
            if(table->col_names_type[j]==STRING){
                fprintf(fp,"%s",table->row_list[i].value[j].valuestring.data);
                printf("%s",table->row_list[i].value[j].valuestring.data);
            }

            if(j < table->used_col_count - 1){
                fprintf(fp,"|");
                printf("|");
            }
        }
        fprintf(fp,"\n");
        printf("\n");
    }


}



/**
 *     BzDB_Table_t table;
    const char* file_path = "C:\\Users\\qianyi\\CLionProjects\\BzDB\\dataset.txt";
    BzDB_create_table_from_txt(&table,file_path);
 * @param table
 * @param file_path
 */
void BzDB_create_table_from_json(BzDB_Table_t *table,const char* file_path)
{
    char buf[6000];
    BzDB_file_operate_read_content( file_path, buf,6000);


    cJSON *root = cJSON_Parse(buf);




    char row_1_str[512] = {0}; // 存放第一行数据的
    char row_2_str[512] = {0}; // 存放第二行数据的
    char data_str[6000] = {0}; // 存放数据的

    // 按照\n进行分割
    int start_index_list[BzDB_MAX_COL_COUNT] = {0};
    int end_index_list[BzDB_MAX_COL_COUNT] = {0};
    int len = 0;
    BzDB_string_split(buf, "\n" , start_index_list,end_index_list,&len);
    int row_index = 1;


    cJSON *title_json = cJSON_GetObjectItem(root,"title");
    if(title_json!=NULL){
        strcpy(row_1_str,title_json->valuestring);
        printf("row_1_str:%s\n",row_1_str);
    }

    cJSON *types_json = cJSON_GetObjectItem(root,"types");
    if(types_json!=NULL){
        strcpy(row_2_str,types_json->valuestring);
    }

    int line_count = 0;
    cJSON *line_count_json = cJSON_GetObjectItem(root,"lines");
    if(line_count_json!=NULL){
        line_count = line_count_json->valueint;
    }


    const char* names[BzDB_MAX_COL_COUNT] ;
    char names_array[BzDB_MAX_COL_COUNT][100] ;

    len = 0;
    BzDB_string_split(row_1_str, "," , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++){
        char temp[255] = {0};
        BzDB_string_sub(row_1_str,start_index_list[i],end_index_list[i],temp);

        strcpy(names_array[i],temp);
        names[i] = (const char*)names_array[i];
    }


    len = 0;
    BzDB_ValueType types[BzDB_MAX_COL_COUNT] ;
    BzDB_string_split(row_2_str, "," , start_index_list,end_index_list,&len);
    for(int i = 0 ; i < len ; i++){
        char temp[255] = {0};
        BzDB_string_sub(row_2_str,start_index_list[i],end_index_list[i],temp);

        if( strcmp(temp,"STRING") == 0 ){
            types[i] = STRING;
        }
        if( strcmp(temp,"INTEGER") == 0 ){
            types[i] = INTEGER;
        }
    }

    // init

    BzDB_Table_t_init(table,len ,names ,types);


    for(int i = 0 ; i < line_count ; i++){

        char temp_str[20] = {0};
        sprintf(temp_str,"row%d",i);

        cJSON *row_json =  cJSON_GetObjectItem(root,temp_str);
        if(row_json!=NULL){
            BzDB_insert_one_row_from_str(table,row_json->valuestring);
        }

    }


}
